package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.config.exception.ResultException;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.InstructionDicRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.InstructionRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Instruction;
import com.ktwlsoft.archivemanage.domain.dao.entity.InstructionDic;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.instruction.InstrctionDicSaveRequest;
import com.ktwlsoft.archivemanage.payload.instruction.InstrctionDicUpdateRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 指南字典
 *
 * @author Joker
 * @version 1.0
 */
@Component
public class InstructionDicService {

    /**
     * 指南字典
     */
    private final InstructionDicRepositoryService instructionDicRepositoryService;
    private final InstructionRepositoryService instructionRepositoryService;


    public InstructionDicService(InstructionDicRepositoryService instructionDicRepositoryService, InstructionRepositoryService instructionRepositoryService) {
        this.instructionDicRepositoryService = instructionDicRepositoryService;
        this.instructionRepositoryService = instructionRepositoryService;
    }

    /**
     * 指南字典分页列表
     *
     * @return Result<?>
     */
    public Result<?> findPage(int pn, int pageSize, String businessFrom) {
        if (businessFrom == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("必须指定使用端");
        }
        PageInfo<InstructionDic> pageInfo = instructionDicRepositoryService.findPage(pn, pageSize, businessFrom);
        return Result.ok(pageInfo);
    }

    /**
     * 指南字典新增
     *
     * @param request request
     * @return Result<?>
     */
    public Result<?> save(InstrctionDicSaveRequest request) {
        InstructionDic instructionDic = new InstructionDic();
        BeanUtils.copyProperties(request, instructionDic);
        try {
            instructionDicRepositoryService.insert(instructionDic);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResultException(ResultType.SAVING_ERROR.getReason());
        }
        return Result.ok(instructionDic.getId());
    }

    /**
     * 指南字典修改
     *
     * @param request request
     * @return Result<?>
     */
    public Result<?> edit(InstrctionDicUpdateRequest request) {
        InstructionDic instructionDic = instructionDicRepositoryService.getById(request.getId());
        if (instructionDic == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        instructionDic.setName(request.getName());
        instructionDic.setLevel(request.getLevel());
        instructionDic.setPid(request.getPid());
        try {
            instructionDicRepositoryService.update(instructionDic);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR);
        }
        return Result.ok(instructionDic.getId());
    }

    /**
     * 指南字典删除
     *
     * @param id id
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> deleteById(Long id) {
        if (id == null || id <= 0) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        InstructionDic instructionDic = instructionDicRepositoryService.getById(id);
        if (instructionDic == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        if (instructionDic.getId() != null) {
            //是否有引用
            List<Instruction> instructions = instructionRepositoryService.getByBelongId(id);
            if (instructions != null && instructions.size() > 0) {
                return Result.body(null, ResultType.CHECK_ERROR).withMessage("无法删除,该数据已被引用");
            }
        }
        //删除子导航
        List<InstructionDic> instructionDicList = instructionDicRepositoryService.getChildrenById(id);
        if (instructionDicList.size() > 0) {
            for (InstructionDic item : instructionDicList) {
                List<Instruction> instructions2 = instructionRepositoryService.getByBelongId(item.getId());
                if (instructions2 != null && instructions2.size() > 0) {
                    return Result.body(null, ResultType.CHECK_ERROR).withMessage("无法删除,子导航已被引用");
                }
                try {
                    instructionDicRepositoryService.deleteById(item.getId());
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new ResultException(ResultType.DELETE_ERROR.getReason(),ResultType.DELETE_ERROR);
                }
            }
        }
        try {
            instructionDicRepositoryService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ResultException(ResultType.DELETE_ERROR.getReason(),ResultType.DELETE_ERROR);
        }
        return Result.ok(instructionDic.getId());
    }

    /**
     * 获取指南导航下的指南列表
     *
     * @param belongId
     * @return
     */
    public Result<?> getByBelongId(int pn, int pageSize, Long belongId) {
        PageHelper.startPage(pn, pageSize);
        PageInfo<Instruction> pageInfo = new PageInfo<>(instructionRepositoryService.getByBelongId(belongId));
        return Result.ok(pageInfo);
    }

    /**
     * 处理value on 2019/11/5 9:46
     *
     * @param instructionDic instructionDic
     * @return 字段存在true, 不存在false
     */
    private boolean handelValue(InstructionDic instructionDic) {
        if (instructionDic.getName() == null || instructionDic.getName() == null || "".equals(instructionDic.getName())) {
            return false;
        }
        List<InstructionDic> instructionDics = instructionDicRepositoryService.getByName(instructionDic);
        if (instructionDics != null && instructionDics.size() > 0) {
            for (InstructionDic dic : instructionDics) {
                if (!dic.getId().equals(instructionDic.getId())) {
                    return true;
                }
            }
        }
        return false;
    }

    public Result<?> getFirstPageList(Integer pn, Integer pageSize, InstructionDic.BusinessFrom businessFrom) {
        PageInfo<InstructionDic> pageInfo = instructionDicRepositoryService.getFirstPageList(pn, pageSize, businessFrom);
        return Result.ok(pageInfo);
    }

    /**
     * @Description 根据上级id获取下级列表
     * @dateTime 2019-12-18 11:19
     * @author wangchong
     */
    public Result<?> getChildrenById(Long id) {
        List<InstructionDic> list = instructionDicRepositoryService.getChildrenById(id);
        return Result.ok(list);
    }
}
